<template>
  <PageWrapper>
    <BasicTable @register="registerTable">
      <template #tableTitle> </template>
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'type'">
          <span class="global-stop-status" v-if="record.type === 0">灰度</span>
          <span class="global-active-status" v-else-if="record.type === 1">生产</span>
        </template>
        <template v-else-if="column.dataIndex === 'phase'">
          <span class="global-active-status" v-if="record.phase === 'Active'">运行中</span>
          <span class="global-stop-status" v-else-if="record.phase === 'Terminating'">终止</span>
        </template>
        <template v-else-if="column.dataIndex === 'labels'">
          <div class="global-txt-ellipsis box-item" v-for="(value, key) in record.labels" :key="key">
            <a-tooltip placement="bottomLeft">
              <template #title>{{ key }} / {{ value }}</template>
              <div class="item-label">
                <div class="label-key">{{ key }}</div>
                <div class="label-value">{{ value }}</div>
              </div>
            </a-tooltip>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'operate'">
          <a-button
            type="link"
            class="g-operate-btn"
            v-auth="permissionCodeMap.namespaceViewYaml"
            @click="openViewYaml(record)"
          >
            查看YAML
          </a-button>
        </template>
      </template>
    </BasicTable>
    <ViewYaml @register="viewYamlRegister" />
  </PageWrapper>
</template>

<script lang="ts" setup>
import { reactive, ref, computed, unref } from 'vue'
import PageWrapper from '@/components/PageWrapper/index.vue'
import { BasicTable, useTable } from '@/components/Table'
import { getNamespaceBasicColumns, getSearchFormConfig } from './dataSource'
import { useDrawer } from '@/components/Drawer'
import ViewYaml from '@/views/deployUpgrade/components/ViewYaml/index.vue'
import { operateActionType, operateTxtType, constantMap } from '@/types/enum'
import { permissionCodeMap } from '@/utils/permissionCodeMap'
import { NamespacedRecord } from '@/types/deployUpgrade'
import { message } from 'ant-design-vue'
import api from '@/api'
const { listNamespaced } = api

const [viewYamlRegister, { openDrawer: viewYamlOpenDrawer, setDrawerProps }] = useDrawer()

const [registerTable, { reload, setPagination, getForm }] = useTable({
  api: listNamespaced,
  columns: getNamespaceBasicColumns(),
  useSearchForm: true,
  pagination: false,
  formConfig: getSearchFormConfig(),
  afterFetch: afterDataFetch,
  fetchSetting: {
    listField: 'list'
  },
  scroll: { y: constantMap.tableYHeight, x: constantMap.tableXWidth }
})

// 表格返回数据处理
function afterDataFetch(data: NamespacedRecord[]) {
  let formData = getForm().getFieldsValue()
  if (formData && formData.name && data && data.length) {
    return data.filter((item: NamespacedRecord) => item.name && item.name.indexOf(formData.name) > -1)
  } else {
    return data
  }
}

const openViewYaml = (record: any) => {
  viewYamlOpenDrawer(true, { record, type: 'namespace' })
}
</script>

<style lang="less" scoped>
.box-item {
  margin-bottom: 8px;
  &:last-child {
    margin-bottom: 0;
  }
}
.item-label {
  color: #fff;
  display: flex;
  align-items: center;
  .label-key {
    padding: 0 8px;
    margin-right: 1px;
    background-color: #c0c0c0;
    border-radius: 10px 0 0 10px;
  }
  .label-value {
    padding: 0 8px;
    background-color: @primary-color;
    border-radius: 0 10px 10px 0;
  }
}
</style>
